package com.jzo2o.foundations.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.service.impl.RegionServiceImpl;
import com.jzo2o.foundations.service.impl.ServeRegionServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2025/2/13 17:03
 * @Version 1.0
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class SpringCacheSyncHandlerTask {

    private final RedisTemplate redisTemplate;
    private final RegionMapper regionMapper;
    private final RegionServiceImpl regionService;
    private final ServeRegionServiceImpl serveRegionService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void RegionList(){
        //删除同前缀的缓存 单独写缓存方法
        clearCacheDate("JZ_CACHE::ACTIVE_REGIONS_*");
        //调用加了@Cacheable注解查询方法 重新添加缓存
        //要先查询所有已开通的地区的ids遍历集合 然后使用id作为参数执行查询方法
        regionService.queryActiveRegionListCache();
    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void serveIcon(){
        clearCacheDate("JZ_CACHE:SERVE_ICON_*");
        //调用加了@Cacheable注解查询方法 重新添加缓存
        //要先查询所有已开通的地区的ids遍历集合 然后使用id作为参数执行查询方法
        LambdaQueryWrapper<Region> qw = new LambdaQueryWrapper<>();
        qw.eq(Region::getActiveStatus,2);
        List<Region> regions = regionMapper.selectList(qw);
        List<String> ids = new ArrayList<>();
        for (Region region : regions) {
            Long id = region.getId();
            ids.add(String.valueOf(id));
        }
        for (String id : ids) {
            serveRegionService.getServeCategoryList(id);
        }

    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void serveType(){
        clearCacheDate("JZ_CACHE:SERVE_TYPE_*");
        //调用加了@Cacheable注解查询方法 重新添加缓存
        //要先查询所有已开通的地区的ids遍历集合 然后使用id作为参数执行查询方法
        LambdaQueryWrapper<Region> qw = new LambdaQueryWrapper<>();
        qw.eq(Region::getActiveStatus,2);
        List<Region> regions = regionMapper.selectList(qw);
        List<Long> ids = new ArrayList<>();
        for (Region region : regions) {
            Long id = region.getId();
            ids.add(id);
        }
        for (Long id : ids) {
            serveRegionService.getAllTypeByRegionId(id);
        }

    }

    @Scheduled(cron = "0 0 0 * * ?")
    public void hotServe(){
        clearCacheDate("JZ_CACHE:HOT_SERVE_*");
        //调用加了@Cacheable注解查询方法 重新添加缓存
        //要先查询所有已开通的地区的ids遍历集合 然后使用id作为参数执行查询方法
        LambdaQueryWrapper<Region> qw = new LambdaQueryWrapper<>();
        qw.eq(Region::getActiveStatus,2);
        List<Region> regions = regionMapper.selectList(qw);
        List<String> ids = new ArrayList<>();
        for (Region region : regions) {
            Long id = region.getId();
            ids.add(String.valueOf(id));
        }
        for (String id : ids) {
            serveRegionService.getHotServeList(id);
        }

    }

    //清除固定前缀开头的所有key缓存
    private void clearCacheDate(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

}